﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Sector7.Framework.Core;
using Sector7.Framework.DataDictionary.CORS;
using Sector7.Framework.Enums;

namespace Sector7.Framework.DataDictionary.Windows
{
    /// <summary>
    /// AddValueDomainItemWindow.xaml 的交互逻辑
    /// </summary>
    public partial class AddValueDomainItemWindow : AddModeWindowBase
    {
        public AddValueDomainItemWindow()
        {
            InitializeComponent();

            FocusedControl = txbValue;
        }

        public S7_ValueDomainItem ValueDomainItem
        {
            get { return m_entity as S7_ValueDomainItem; }
            set { m_entity = value; }
        }

        protected override void AutoAdd()
        {
            base.AutoAdd();

            try
            {
                string value = txbValue.Text;
                if (value.Length != m_addValueFormat.Length)
                {
                    m_addValueFormat = "{0:" + new string('0', value.Length) + "}";
                }

                m_addValue = Convert.ToInt32(value);
                m_addValue++;

                FocusedControl = txbImplication;
            }
            catch (Exception e)
            {
                Log.WriteLine(LogSeverityType.WRN, "DAD", "该值不能使用递增功能。");
                Log.WriteLine(LogSeverityType.WRN, "DAD", e);
                m_addValue = -1;
            }
        }

        protected override void InitializeForCreating()
        {
            txbValue.Text = AddedValue;
            txbImplication.Text = string.Empty;
            txbDescription.Text = string.Empty;

            m_entity = new S7_ValueDomainItem();

            base.InitializeForCreating();
        }

        protected override void InitializeForUpdating()
        {
            txbValue.Text = ValueDomainItem.DomainItemValue;
            txbImplication.Text = ValueDomainItem.DomainItemImplication;
            txbDescription.Text = ValueDomainItem.DomainItemDescription;

            btnContinue.Visibility = Visibility.Hidden;

            base.InitializeForUpdating();
        }

        protected override void AddData()
        {
            ValueDomainItem.DomainItemDescription = txbDescription.Text;
            
            base.AddData();
        }

        protected override bool CheckData()
        {
            m_errors.Clear();

            ValueDomainItem.DomainItemValue = txbValue.Text;
            ValueDomainItem.DomainItemImplication = txbImplication.Text;

            if (string.IsNullOrEmpty(ValueDomainItem.DomainItemValue))
            {
                m_errors.Add("值", "不能为空！");
            }
            if (string.IsNullOrEmpty(ValueDomainItem.DomainItemImplication))
            {
                m_errors.Add("值含义", "不能为空！");
            }

            return base.CheckData();
        }
    }
}
